package com.foxzzz.binary.search;

public class Solution704 {
    public int search(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;
        while (low <= high) {
            int mid = (int) ((long) (low + high) / 2);
            if (mid == nums.length) {
                break;
            } else if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution704().search(new int[]{-1, 0, 3, 5, 9, 12}, 9));
        System.out.println(new Solution704().search(new int[]{-1, 0, 3, 5, 9, 12}, 2));
        System.out.println(new Solution704().search(new int[]{-1, 0, 3, 5, 9, 12}, 13));
    }
}
